package com.lujianfei.virusbroadcast.beans;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.lujianfei.virusbroadcast.Constants;
import com.lujianfei.virusbroadcast.cache.SPRepository;

/**
 * 医院
 * <p>
 * 床位容量
 *
 * @ClassName: Hospital
 * @Description: 医院，包含床位容量
 * @author: Bruce Young
 * @date: 2020年02月02日 20:58
 */
public class Hospital extends Point {
    private int width;
    private int height = 600;

    public int getWidth() {
        return width;
    }


    public int getHeight() {
        return height;
    }


    private static Hospital hospital = new Hospital();

    public static Hospital getInstance() {
        return hospital;
    }

    private List<Bed> beds = new ArrayList<>();

    /**
     * 获取所有床位
     *
     * @return
     */
    public List<Bed> getBeds() {
        return beds;
    }

    private Hospital() {
        //医院矩形所在坐标
        super();
        //根据床位数量调整医院矩形的大小
        if (SPRepository.getInstance().getBedCount() == 0) {
            width = 0;
            height = 0;
        }
    }
    
    @Override
    public void setX(int x) {
    	// TODO Auto-generated method stub
    	super.setX(x);
    	resetBedPosition();
    }
    
    @Override
    public void setY(int y) {
    	// TODO Auto-generated method stub
    	super.setY(y);
    	resetBedPosition();
    }


	public void resetBedPosition() {
		beds.clear();
    	//根据医院床位数量计算医院宽度
        //因为高度定了只能装载100个床位
        int column = SPRepository.getInstance().getBedCount() / 100;
        width = column * 6;
    	  //根据第一个床位坐标初始化其他床位的坐标
        for (int i = 0; i < column; i++) {

            for (int j = 10; j <= 606; j += 6) {

                Bed bed = new Bed(getX() + i * 6, getY() + j - 10);
                beds.add(bed);
                if (beds.size() >= SPRepository.getInstance().getBedCount()) {//确定医院床位承载数量
                    break;
                }
            }

        }
	}

    /**
     * 使用床位
     *
     * @return
     */
    public Bed pickBed() {
        for (Bed bed : beds) {
            if (bed.isEmpty()) {
                return bed;
            }
        }
        return null;
    }

    /**
     * 死亡或痊愈出院空出床位
     *
     * @param bed
     * @return
     */
    public Bed returnBed(Bed bed) {
        if (bed != null) {
            bed.setEmpty(true);
        }
        return bed;
    }
}
